import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    beforeEnter: (to, from, next) => {
      console.log('进入首页路由')
      next()
    }
  },
  {
    path: '/food',
    name: 'Food',
    component: () => import('@/views/FoodWheel.vue')
  },
  {
    path: '/blog',
    name: 'Blog',
    component: () => import('@/views/Blog.vue')
  },
  {
    path: '/chat',
    name: 'Chat',
    component: () => import('@/views/Chat.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/About.vue')
  },
  {
    path: '/contact',
    name: 'Contact',
    component: () => import('@/views/Contact.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresGuest: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { requiresGuest: true }
  },
  {
    path: '/write',
    name: 'WriteArticle',
    component: () => import('@/views/WriteArticle.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/post/:id',
    name: 'Post',
    component: () => import('@/views/Post.vue'),
    props: true
  },
  
  // 管理员路由
  {
    path: '/admin',
    component: () => import('@/views/admin/AdminDashboard.vue'),
    meta: { requiresAdmin: true },
    children: [
      {
        path: '',
        redirect: 'dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/DashboardHome.vue')
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('@/views/admin/UserManagement.vue')
      },
      {
        path: 'articles',
        name: 'ArticleManagement',
        component: () => import('@/views/admin/ArticleManagement.vue')
      },
      // {
      //   path: 'articles/create',
      //   name: 'ArticleCreate',
      //   component: () => import('@/views/admin/ArticleEdit.vue')
      // },
      // {
      //   path: 'articles/edit/:id',
      //   name: 'ArticleEdit',
      //   component: () => import('@/views/admin/ArticleEdit.vue'),
      //   props: true
      // },
      // {
      //   path: 'settings',
      //   name: 'AdminSettings',
      //   component: () => import('@/views/admin/Settings.vue')
      // }
    ]
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/admin/AdminLogin.vue'),
    meta: { requiresGuest: true }
  },
  
  // // 404 页面
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: 'NotFound',
  //   component: () => import('@/views/NotFound.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 需要认证的路由 (普通用户)
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!userStore.isLoggedIn) {
      return next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
    return next()
  }
  
  // 需要管理员权限的路由
  if (to.matched.some(record => record.meta.requiresAdmin)) {
    if (!userStore.isLoggedIn) {
      return next({
        path: '/admin/login',
        query: { redirect: to.fullPath }
      })
    }
    
    if (!userStore.isAdmin) {
      // 已登录但非管理员，跳转到无权限提示页或用户首页
      return next({
        path: '/',
        query: { from: to.path }
      })
    }
    return next()
  }
  
  // 需要访客状态的路由
  if (to.matched.some(record => record.meta.requiresGuest)) {
    if (userStore.isLoggedIn) {
      // 根据用户类型跳转不同首页
      return next(userStore.isAdmin ? '/admin/dashboard' : '/')
    }
    return next()
  }
  
  next()
})

export default router